<?php

namespace App\Jobs;

use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Common\Service\RedisService;

class Visits implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
    protected $data;
    public $tries = 1;//任务最大尝试次数。

    /**
     * Create a new job instance.
     * @return void
     */
    public function __construct($data)
    {
        $this->data = $data;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        try {
            //存入一条访问记录
            $model = new \Common\Model\Visits();
            $model->create(['ip' => $this->data]);

            //删除现有的redis  KEy
            $redis = new RedisService();
            $redis->del($this->data);
        } catch (\Exception $exception) {
            echo $exception->getMessage();
        }
    }
}
